package com.jsz.peini.server.thread;

import java.util.Date;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jsz.peini.bean.order.OrderInfo;
import com.jsz.peini.bean.task.TaskInfo;
import com.jsz.peini.common.util.SpringContextUtil;
import com.jsz.peini.mapper.order.OrderInfoMapper;
import com.jsz.peini.mapper.task.TaskInfoMapper;

public class TaskFinishTimerTask extends TimerTask {
	private static final Logger logger = LoggerFactory.getLogger(TaskFinishTimerTask.class);
	
	OrderInfoMapper orderInfoMapper = (OrderInfoMapper)SpringContextUtil.getBean(OrderInfoMapper.class);
	
	TaskInfoMapper taskInfoMapper = (TaskInfoMapper)SpringContextUtil.getBean(TaskInfoMapper.class);
	
	//任务ID
	private Integer taskId;
	//要查的用户userId
	private String userId;
	//甄别是查发起人还是接收人 1:发起人 2:接收人
	private Integer personType;
	@Override
	public void run() {
						OrderInfo orderInfo = orderInfoMapper.selectUserOrderByUserId(getUserId(), getTaskId());
						
						
						if (orderInfo != null && orderInfo.getPayStatus().intValue() != 1) {//订单生成但是未支付
							
							OrderInfo order = new OrderInfo();
							order.setId(orderInfo.getId());
							order.setOrderStatus(-1);
							orderInfoMapper.updateByPrimaryKeySelective(order);
							
							if (getPersonType() == 1) {
								TaskInfo taskInfo = new TaskInfo();
								taskInfo.setId(getTaskId());
								taskInfo.setTaskStatus(4);
								taskInfo.setTaskEndTime(new Date());
								taskInfoMapper.updateByPrimaryKeySelective(taskInfo);
								logger.info("发起人未支付,订单被取消,任务已完成");
							}else if (getPersonType() == 2) {
								TaskInfo taskInfo = new TaskInfo();
								taskInfo.setId(getTaskId());
								taskInfo.setTaskOtherStatus(41);
								taskInfo.setTaskEndTime(new Date());
								taskInfoMapper.updateByPrimaryKeySelective(taskInfo);
								logger.info("接收人未支付,订单被取消,任务已完成");
							}
						}else if (orderInfo == null) {//没有订单时
							
							if (getPersonType() == 1) {
								TaskInfo taskInfo = new TaskInfo();
								taskInfo.setId(getTaskId());
								taskInfo.setTaskStatus(4);
								taskInfo.setTaskEndTime(new Date());
								taskInfoMapper.updateByPrimaryKeySelective(taskInfo);
								logger.info("发起人未支付,订单被取消,任务已完成");
							}else if (getPersonType() == 2) {
								TaskInfo taskInfo = new TaskInfo();
								taskInfo.setId(getTaskId());
								taskInfo.setTaskOtherStatus(41);
								taskInfo.setTaskEndTime(new Date());
								taskInfoMapper.updateByPrimaryKeySelective(taskInfo);
								logger.info("接收人未支付,订单被取消,任务已完成");
							}
							
						}
						
						
						
						
	}


	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public Integer getTaskId() {
		return taskId;
	}
	public void setTaskId(Integer taskId) {
		this.taskId = taskId;
	}


	public Integer getPersonType() {
		return personType;
	}


	public void setPersonType(Integer personType) {
		this.personType = personType;
	}
}
